package com.leetcodehot.problems;

import java.util.ArrayDeque;
import java.util.Deque;

public class problems1609 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isEvenOddTree(TreeNode root) {
        int level = 0;
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.addLast(root);
        while (!deque.isEmpty()) {
            int n = deque.size();
            TreeNode pre = null;
            while (n-- > 0) {
                TreeNode node = deque.removeFirst();
                if (level % 2 == 0) {
                    if (node.val % 2 == 0 || (pre != null && pre.val >= node.val)) {
                        return false;
                    }
                }
                if (level % 2 == 1) {
                    if (node.val % 2 == 1 || (pre != null && pre.val <= node.val)) {
                        return false;
                    }
                }
                pre = node;
                if (node.left != null) {
                    deque.addLast(node.left);
                }
                if (node.right != null) {
                    deque.addLast(node.right);
                }
            }
            level++;
        }
        return true;
    }
}
